package com.boyaa.shells.tool.slider
{
	// 内部导入类
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	/**
	 * 列表滚动逻辑处理
	 * @author Kevin
	 */
	public class ScrollControl
	{
		////////////////////////////////////////////////////////////////////////////////
		// 常量			
		public static const SORT_LIST:String = "sortList";
		public static const SortEvent:EventDispatcher = new EventDispatcher();		
		
		////////////////////////////////////////////////////////////////////////////////
		//
		private var _listView:MovieClip = null;			
		private var _itemList:Array = null;				// 房间列表		
		private var _slider:EasySlider = null;			// 滑块		
		private var _listMask:MovieClip = null;			// 房间蒙版
		
		////////////////////////////////////////////////////////////////////////////////
		// 运行时参数
		private var _line:int = 0;						// 已滚动到的行
		private var _currentLine:int = 0;				// 当前行
		private var _prevLine:int = 0;					//
		
		private var _scrollHeight:Number = 0;			// 
		
		private var _lineCount:int = 0;					// 总行数
		private var _dataList:Array = null;				// 房间列表
		
		private var _currentValue:Number = 0;			// 滑块当前值
		private var _currrentItem:BasicItem = null;		// 被选中的房间
		private var _selectedIndex:int = 0;				// 被选中行的索引		
		
		////////////////////////////////////////////////////////////////////////////////
		// 属性		
		private var _prevY:Number = 0;
		private var _isDown:Boolean = true;				// 是否向下滚动	
		
		private var _listParent:IListParent = null;		// 
		
		private var _itemHeight:Number = 0;				// 当行高度
		private var _lineSpace:Number = 0;				// 行距
		private var _headBarHeight:Number = 0;			// 标题栏高度
		private var _pageCount:int = 0;					// 每页显示的行数
		private var _columnCount:int = 0; 				// 列表的列数
		
		private var _listConatainer:Sprite = null;
		private var _y:Number = 0;		
		
		private var _topMargin:Number = 0;				// 顶部偏移		
		
		/**
		 * 构造函数
		 * @param	parent
		 * @param	listparent
		 * @param	columnCount
		 * @param	pageCount
		 * @param	headBarHeight
		 */
		public function ScrollControl(listView:MovieClip, listParent:IListParent, 
										columnCount:int = 0, pageCount:int = 0,
										headBarHeight:Number = 0)		
		{
			_listView = listView;
			
			//
			_listParent = listParent;
			_columnCount = columnCount;
			_pageCount = pageCount + 1;
			_headBarHeight = headBarHeight;			
			
			//
			_listConatainer = new Sprite();
			_y = _listView["listItem" + 1].y;
			_listConatainer.y = _y;
			_listView.addChild(_listConatainer);			
			
			// 设置蒙版
			_listMask = _listView["listMask"];			
			_listMask.alpha = 0;			
			_listConatainer.mask = _listMask;
			_listMask.mouseEnabled = false;								
			
			// 初始化房间
			initListItem();	
			
			// 滑块
			_slider = new EasySlider(_listView["sliderView"]);			
			
		}
		
		//------------------------------------------------------------------------------------------------------//
		//												公开API													//
		//------------------------------------------------------------------------------------------------------//
		/**
		 * 添加事件侦听
		 */
		public function addEvent():void
		{		
			_slider.addEventListener(Event.CHANGE, changeHandler);		
			ScrollControl.SortEvent.addEventListener(ScrollControl.SORT_LIST, sortListHandler);			
			_listConatainer.addEventListener(Event.SELECT, selectHandler);
		}
		
		/**
		 * 移除事件侦听
		 */
		public function removeEvent():void
		{
			removeListener();
			//_slider.removeEvent();			
			_slider.removeEventListener(Event.CHANGE, changeHandler);
			
			ScrollControl.SortEvent.removeEventListener(ScrollControl.SORT_LIST, sortListHandler);
			//
			_listConatainer.removeEventListener(Event.SELECT, selectHandler);
		}
		
		/**
		 * 清空当前选择的列表行
		 */
		public function clearSelectedItem():void
		{
			if(_currrentItem)
				_currrentItem.select(false);
			_currrentItem = null;
			_selectedIndex = -1;
		}
		
		/**
		 * 列表排序时更新显示
		 * @param	e
		 */
		private function sortListHandler(e:Event):void 
		{
			if (_currrentItem == null)
				return;
			//
			setSelectedItem();				
		}
		
		/**
		 * 列表行被选中
		 * @param	e
		 */
		private function selectHandler(e:Event):void 
		{	
			e.stopPropagation();
			
			// 取出被点击列表行
			var item:BasicItem = BasicItem.clickTarget;
			
			//
			if (_currrentItem)
				_currrentItem.select(false);
			_currrentItem = item;
			_currrentItem.select(true);
			_selectedIndex = _currrentItem.id;			
			
		}
		
		/**
		 * 设置蒙版宽度
		 * @param	maskWidth		遮罩宽度
		 * @param	isCenterAlign	滑块资源是否为居中对齐
		 */
		public function setMaskWidth(maskWidth:Number,isCenterAlign:Boolean = true):void
		{
			_listMask.width = maskWidth;					
			//_slider.x = _listMask.x + _listMask.width + (isCenterAlign?_slider.width / 2 : 0);				
		}
		
		/**
		 * 更新数据列表总行数
		 * @param	lineCount
		 * @param	offset
		 */
		public function setDataList(dataList:Array, offset:int = 0, isShowList:Boolean = true):void			
		{
			if (dataList == null)
				dataList = [];
			//
			_dataList = dataList;				
			_lineCount =_dataList.length;
			
			//
			//throw new Error(this + _dataList.length + _pageCount);
			
			if (isShowList == false) return;			
			
			//
			_slider.setBarHeight(_pageCount - 1, _lineCount);
			_slider.setScrollRange(offset);
			
			//_prevLine = 0;
			setNewList();
			
			_slider.value = 0;
		}
		
		/**
		 * 设置列表的宽度
		 * @param	widths	数组：含有COLUMN个元素
		 */
		public function setColumnsWidth(widths:Array):void
		{
			var item:BasicItem = null;
			for (var i:int = 0; i < _itemList.length; i++)
			{
				item = _itemList[i];
				item.setColumnsWidth(widths);
			}
		}
		
		/**
		 * 返回高度
		 * @return
		 */
		public function getSliderWidth():Number
		{
			return _slider.width;
		}
		//------------------------------------------------------------------------------------------------------//
		/**
		 * 滚动处理
		 * @param	e
		 */
		private function changeHandler(e:Event):void 
		{				
			_currentValue = _slider.value;
			
			_scrollHeight = _slider.value * (_lineCount - _pageCount + 1) * _itemHeight / 100;
			_listConatainer.y =_y - _scrollHeight;				
			_isDown = _listConatainer.y < _prevY;				
			
			//
			if (_listConatainer.y != _prevY)
				_prevY = _listConatainer.y;
			//
			_line = Math.floor(_scrollHeight / _itemHeight);			
			if (_line != _prevLine)			
			{			
				_prevLine = _line;	
				setNewOrder(_isDown);	
				setSelectedItem();
			}
		}
		
		/**
		 * 设置以选中列表行
		 */
		private function setSelectedItem():void
		{			
			if (_currrentItem == null)
				return;	
			//
			if (findSelectedItem())
			{					
				_currrentItem.select(true);	
				return;
			}			
			_currrentItem.select(false);
			
		}
		
		/**
		 * 寻找已选择的行
		 */
		private function findSelectedItem():Boolean
		{
			if (_currrentItem.id == _selectedIndex)
				return true;
			//
			var listItem:BasicItem = null;
			for (var i:int = 0; i < _itemList.length; i++)
			{
				listItem = _itemList[i];
				if (listItem.id != _selectedIndex)
					continue;
				//
				if (_currrentItem)
					_currrentItem.select(false);
				_currrentItem = listItem;						
				return true;
			}		
			return false;
		}
		
		/**
		 * 设置新的顺序
		 */
		private function setNewOrder(isDown:Boolean = true):void
		{	
			if (_line == _currentLine)
				return;
			_currentLine = _line;
			
			
			if(isDown)
			{
				_itemList.push(_itemList[0]);
				_itemList.shift();
			}
			else
			{
				_itemList.unshift(_itemList[_itemList.length - 1]);
				_itemList.pop();
			}
			
			setNewList();
		}
		
		/**
		 * 获得新的房间排序列表
		 */
		private function setNewList(isAdd:Boolean = true):void
		{			
			var listItem:BasicItem = null;
			var itemData:Object = null;
			
			var j:int = 0;	
			var pos:Number =_topMargin + _itemHeight * _line;
			for (var i:int = _line ; i < _pageCount + _line ; i++)			
			{
				listItem = _itemList[j];
				listItem.y = pos;
				pos += _itemHeight + _lineSpace;
				
				j++;
				
				//
				if (isAdd == false)
					continue;	
				//
				itemData = _dataList[i];
				if (itemData == null)
					listItem.removeData();			
				else			
					listItem.readData(itemData, i);			
				
			}	
		}
		
		/**
		 * 初始化房间
		 */
		private function initListItem():void
		{
			_itemList = [];			
			var listItem:BasicItem = null;		
			for (var i:int = 1; i <= _pageCount; i++)			
			{					
				listItem = _listParent.creatItem(_listView["listItem" + i], _columnCount) as BasicItem;						
				listItem.setParent(_listConatainer);
				_itemList.push(listItem);
				
			}
			
			//
			_itemHeight = listItem.height + _lineSpace;			
			
			//
			setNewList(false);
		}	
		
		
		/**
		 * 是否添加事件侦听 
		 */
		private function removeListener():void
		{
			var listItem:BasicItem = null;
			for (var i:int = 0; i < _itemList.length; i++)
			{
				listItem = _itemList[i];					
				listItem.removeData();	
			}
		}
		
		//------------------------------------------------------------------------------------------------------//
		//											Getters & Setters											//
		//------------------------------------------------------------------------------------------------------//			
		/**
		 * 房间Y坐标
		 */
		public function get y():Number { return _listView.y; }		
		public function set y(value:Number):void 
		{			
			_listView.y = value;			
		}
		
		/**
		 * 房间X坐标
		 */
		public function get x():Number { return _listView.x; }		
		public function set x(value:Number):void 
		{			
			_listView.x = value;			
		}
		
		/**
		 * 高度
		 */
		public function get height():Number
		{
			return _itemHeight * (_pageCount - 1);
		}
		
		/**
		 * 行距
		 */
		public function get lineSpace():Number { return _lineSpace; }		
		public function set lineSpace(value:Number):void 
		{
			_lineSpace = value;
		}		
		
		/**
		 * 顶部偏移
		 */
		public function get topMargin():Number { return _topMargin; }
		public function set topMargin(value:Number):void
		{
			_topMargin = value;
		}
		
		/**
		 * 遮罩高度偏移
		 */
		public function set maskHeightOffset(value:Number):void 
		{
			_listMask.height += value;
		}
		
		public function get itemList():Array { return _itemList; }
		
	}	
	
}